<?php   
namespace Common\Service\Pay;
use Common\ServiceInterface\PayService;
use Think\Log;
require_once "pay/wxpay/lib/WxPay.Api.php";
require_once 'pay/wxpay/lib/WxPay.Notify.php'; 
/**
 * 微信支付服务接口
 * @author liuyang
 *
 */  
class WxpayService implements PayService {
 
    public static function unifiedorder($data,$config)
    { 
        $openId=isset($data['openid'])?$data['openid']:"";
        $product_id=isset($data['product_id'])?$data['product_id'] :""; 
        //统一下单
        $input = new \WxPayUnifiedOrder();
        $input->SetBody(isset($data['body'])? $data['body'] : "");
        $input->SetAttach(isset($data['attach']) ? $data['attach'] : "");
        $input->SetOut_trade_no($data['out_trade_no']); 
        //以分数为准
        $input->SetTotal_fee($data['total_fee']*100);
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 600)); 
        $input->SetGoods_tag(isset($data['good_tag']) ? $data['good_tag'] :"");
        $input->SetNotify_url($config['NOTIFY_URL']); 
        $tradeType = "APP";
        if($data['tradeType'] != null)
            $tradeType = $data['tradeType'];
        $input->SetTrade_type($tradeType);
        $input->SetOpenid($openId);
        $input->SetProduct_id($product_id); 
        $result = \WxPayApi::unifiedOrder($input,20,$config);     
        return $result;
    }
    /**
     * 封闭APP签名参数
     * @param unknown $result
     * @return multitype:string unknown number
     */
    private static function _getAppSignParam($result){
        $data =array();
        $data["appid"]= $result["appid"];
        $data["partnerid"]= $result["mch_id"];
        $data["noncestr"]= $result['nonce_str']; 
        $data["prepayid"]= $result["prepay_id"];
        $data['package']="Sign=WXPay"; 
        $data['timestamp']=time();
        return $data;
    }
    
    /**
     * 封闭JSAPI签名参数
     * @param unknown $result
     * @return multitype:string unknown number
     */
    private static function _getJsapiSignParam($result){
        $data =array();
        $data["appId"]= $result["appid"]; 
        $timeStamp=time();
        $data['timeStamp']="$timeStamp";
        $data["nonceStr"]= $result['nonce_str'];
        $data['signType']="MD5"; 
        $data['package']="prepay_id=".$result["prepay_id"]; 
        return $data;
    }
    
    public  function getSignInfo($data,$config)
    { 
        $config['REMOTE_ADDR']="121.9.242.204";
        $tradeType=$data['tradeType'];  
        $openid ="";
        $openid = $data["openid"];
        $product_id = $data["product_id"];  
        //统一下单  
        $result = self::unifiedorder($data,$config);     
        if(!array_key_exists("appid", $result) ||
            !array_key_exists("mch_id", $result) ||
            !array_key_exists("prepay_id", $result))
        { 
            $msg = "统一下单失败";
            return false;
        }
        $orderno=$data['out_trade_no'];
        
        $data =array();
        if($tradeType==="JSAPI"){
            $data=self::_getJsapiSignParam($result);
        }else{ 
            $data = self::_getAppSignParam($result);
        } 
        $dataResultObj=new \WxPayResults();
        $dataResultObj->FromArray($data); 
        $sign=$dataResultObj->MakeSign($config);
        $data["sign"]= $sign;
        $data['out_trade_no']=$orderno;
        return $data;
    } 
    
	   
}